<?php
namespace app\demo\controller;
use think\Controller;
use think\Db;
use think\Request;
use  app\demo\model\Brands;
class Brand extends Controller{
    public function fenye(){
        $sou=input('zhi');
        $p=Request::instance()->param('p',1);
        $size=Request::instance()->param('size');
        $where="";
        $where="brand_name like '%$sou%' or brand_desc like '%$sou%' or site_url='$sou' or sort='$sou'";
        //调用model层
        $login=new Brands();
        $res=$login->index($p,$size,$where);
        if($res){
            return['code'=>1,'msg'=>'分页成功','data'=>$res];
        }
    }
    //展示品牌
    public function first(){
        $p=Request::instance()->param('p',1);
        $size=Request::instance()->param('size',3);
        $login=new Brands();
        $data=$login->index($p,$size,"");
//        $res=Db::table('brand')->select();
        $this->assign('show',$data['show']);
        $this->assign('last',$data['last']);
        $this->assign('next',$data['next']);
        $this->assign('p',$data['p']);
        $this->assign('size',$data['size']);
        $this->assign('sumpage',$data['sumpage']);
        $this->assign('count',$data['count']);
        return view('brand_list');
    }
    //品牌添加展示
    public function adds(){
        return view('brand_add');
    }
    //添加
    public function plus(){
       $res = Request::instance()->param();
        $res['brand_logo']=$this->upload();
        $data=Db::table('brand')->insert($res);
        if($data){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    //删除
    public function del(){
        $id=input('delid');
        if(!empty($id)){
            $res=Db::table('brand')->delete($id);
        }
        if($res){
            return['code'=>1];

        }else{
            return['code'=>0];
        }
    }
    public function saves(){
        $id = input('id');
        $isshow=input('isshow');
        if($isshow==0){
            $data['is_show']=1;
        }else{
            $data['is_show']=0;
        }
        $res=Db::table('brand')->where(['brand_id'=>$id])->update($data);
        if($res){
            return['isshow'=>$data['is_show'],'code'=>1];
        }else{
            return['code'=>0];
        }
    }
    public function jdjg(){
        $cid=input('cid');
        $data['brand_name']=input('changed');
        $res=Db::table('brand')->where('brand_id',$cid)->update($data);
        if($res){
            return['code'=>1,'msg'=>'几点技改成功','data'=>$data['brand_name']];
        }else{
            return['code'=>0,'msg'=>'几点技改成功','data'=>''];
        }
    }
    //编辑修改信息
    public function edit(){
        $brandid=input('brand_id');
        $res=Db::table('brand')->where('brand_id',$brandid)->find();
        $this->assign('data',$res);
        return view('brand_edit');
    }//修改
    public  function gai(){
        $data=Request::instance()->param();
        $img=$this->upload();
        if($img){
            $data['brand_logo']=$img;
        }
        $res=Db::table('brand')->update($data);
        if($res){
            $this->success('修改成功','first');
        }else{
            $this->error('修改失败');
        }
    }
    public function upload(){
            // 获取表单上传文件 例如上传了001.jpg
        if(!$_FILES['brand_logo']){
            return false;
        }
        $file = request()->file('brand_logo');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            return  $info->getFilename();
        }else{
            return $file->getError();
        }
    }
//    public function sou(){
//        $sou=input('zhi');
//        $where="";
//        $where="brand_name like '%$sou%' or brand_desc like '%$sou%' or site_url='$sou' or sort='$sou'";
//        $res=Db::table('brand')->where($where)->select();
//        if($res){
//            return ['node'=>1,'msg'=>'搜索成功','data'=>$res];
//        }else{
//            return ['node'=>0,'msg'=>'搜索失败','data'=>''];
//        }
//    }
}